package com.terry.test2.dp;

import com.terry.test2.old.InsufficientFundsException;
import com.terry.test2.old.InvalidCurrencyException;
import lombok.Data;

import javax.xml.bind.ValidationException;

/**
 * @author 小八
 * @date 2023年02月01日14:22
 */
@Data
public class Account {

    private AccountId accountId;

    private AccountNumber accountNumber;

    private UserId userId;

    private Money available;

    private Money dailyLimit;

    public Currency getCurrency(){
        return this.available.getCurrency();
    }

    //转出
    public void withdraw(Money money) throws ValidationException {
        //先判断汇率是否一样
        if(!money.getCurrency().equals(available.getCurrency())){
            throw new InvalidCurrencyException();
        }
        //判断是否余额够
        if(this.getAvailable().compareTo(money)<0){
            throw new InsufficientFundsException();
        }
        //判断是否被限制了
        if(this.getDailyLimit().compareTo(money)<0){
            throw new InsufficientFundsException();
        }
        this.available.subtract(money);
    }

    //存入
    public void deposit(Money money){
        //先判断汇率是否一样
        if(!money.getCurrency().equals(available.getCurrency())){
            throw new InvalidCurrencyException();
        }
        this.available.add(money);
    }
}
